/*
 * FrameWork.java
 *
 * Created on 28 de Setembro de 2006, 16:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package br.ifrr.gepro.util;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author Paulo Adriano
 */

public class FrameWork {
    
    //Vari�veis do Globais de usu�rios do Sistema  
 
    public static String modulosAcoes;
    public static Date data;
    
    
    //M�todo respons�vel por limpar os campos do formul�rio
    public static void limpaCampos(JPanel jPanel){
        Component[] components = jPanel.getComponents();
        for(int i=0; i<components.length; i++){
            if(components[i] instanceof JTextField){
                JTextField textField = (JTextField) components[i];
                textField.setText("");
            }else if(components[i] instanceof JComboBox){
                JComboBox comboBox = (JComboBox) components[i];
                comboBox.setSelectedIndex(-1);
            }else if(components[i] instanceof JCheckBox){
                JCheckBox checkBox = (JCheckBox) components[i];
                checkBox.setSelected(false);
            }else if(components[i] instanceof JFormattedTextField){
                JFormattedTextField formattedText = (JFormattedTextField) components[i];
                formattedText.setText("");
            }
        }
    }
    
    //M�todo respons�vel por dar focus a um campo JTextField
    public static void focus(Component component){
            if(component instanceof JFormattedTextField){
                JFormattedTextField formattedText = (JFormattedTextField) component;
                formattedText.requestFocus();
            }else if(component instanceof JTextField){
                JTextField jTextField = (JTextField) component;
                jTextField.requestFocus();
            }
    }
    
    //M�todo respons�vel por centralizar um JDialog ou JFrame
    public static void centralizar(Component dialog){
        /*Capturando a resolu��o do desktop do sistema operacional*/
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int w = dialog.getSize().width;
        int h = dialog.getSize().height;
        int x = (screenSize.width-w)/2;
        int y = (screenSize.height-h)/2;
        dialog.setLocation(x, y);      
    }
    
    //M�todo respons�vel por lan�ar uma mensagem de retorno
    public static int confirMsg(){
        Object[] options = { "Confirmar", "Cancelar" };
        return(JOptionPane.showOptionDialog(null, "Clique Confirmar para continuar", "Informa��o", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]));
    }
    
    //Função para criar hash da senha informada  
    public static String md5(String senha){  
       String sen = "";  
       MessageDigest md = null;  
       try {  
          md = MessageDigest.getInstance("MD5");  
       } catch (NoSuchAlgorithmException e) {  
          e.printStackTrace();  
       }  
       BigInteger hash = new BigInteger(1, md.digest(senha.getBytes()));  
       sen = hash.toString(16);           
       return sen;  
    }
}
